package com.itheima.health.service.impl;

import com.itheima.health.common.MessageConst;
import com.itheima.health.dao.OrderSettingDao;
import com.itheima.health.exception.BusinessRuntimeException;
import com.itheima.health.pojo.OrderSetting;
import com.itheima.health.service.OrdersettingService;
import com.itheima.health.util.POIUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;

/**
 * 上传文件Service实现类
 *
 * @author hongBoZ
 * @since 2022/11/22 19:13
 */
@Service
public class OrdersettingServiceImpl implements OrdersettingService {

    @Autowired
    private OrderSettingDao ordersettingDao;

    /**
     * 文件上传
     *
     * @param excelFile
     */
    @Override
    public void upload(MultipartFile excelFile) {
        try {
            List<String[]> lists = POIUtils.readExcel(excelFile);
            if (lists != null && lists.size() > 0) {
                List<OrderSetting> orderSettingList = new ArrayList<>();
                HashSet<Date> set = new HashSet<>();
                for (String[] strings : lists) {
                    //将String集合中的数据转为OrderSetting集合中的数据
                    OrderSetting orderSetting = new OrderSetting(new Date(strings[0]), Integer.valueOf(strings[1]));
                    //每次转换后进行添加
                    orderSettingList.add(orderSetting);
                    set.add(new Date(strings[0]));
                }
                //判断是否存在重复日期
                Boolean result = set.size() == orderSettingList.size() ? true : false;
                if (!result) {
                    //存在抛出异常
                    throw new BusinessRuntimeException("上传的数据存在相同日期，请修改后重新上传");
                }
                //判断数据库中是否已经上传过当日数据
                int count = this.ordersettingDao.count(orderSettingList);
                if (count == 0) {
                    this.ordersettingDao.add(orderSettingList);
                    return;
                }
                //已经存在，抛出异常
                throw new BusinessRuntimeException("已存在当日数据，请查询后重新上传");
            }
        } catch (IOException e) {
            e.printStackTrace();
            throw new BusinessRuntimeException(MessageConst.IMPORT_ORDERSETTING_FAIL);
        }
    }

    /**
     * 预约设置显示
     * @param moth
     * @return
     */
    @Override
    public List<OrderSetting> getOrderSettingByMonth(String moth) {
        return ordersettingDao.getOrderSettingByMonth(moth);
    }
    /**
     * 根据指定日期修改可预约人数
     * @param orderSetting
     * @return
     */
    @Override
    public void editNumberByDate(OrderSetting orderSetting) {
        List<OrderSetting>  list = new ArrayList<>();
        list.add(orderSetting);
        int count =
                ordersettingDao.count(list);
        if(count > 0){
            //当前日期已经进行了预约设置，需要进行修改操作
            ordersettingDao.editNumberByOrderDate(orderSetting);
        }else{
            //当前日期没有进行预约设置，进行添加操作
            ordersettingDao.add(list);
        }
    }
}
